using System;
using WeifenLuo.WinFormsUI.Docking;
using System.Windows.Forms;
using System.Collections.Generic;
using dpatoolkit.UILayer;
using BusinessLayer;

namespace dpatoolkit.UILayer
{
	/// <summary>
	/// 
	/// </summary>
	public class DockPanelMdiManager : IMDIManager
	{
		private DockPanel dockPanel;
		private ToolBox toolBox ;
		private ProjectTreeView projectTreeView ;
        private ToolStripStatusLabel statusLabel;
        private Timer statusDisplayTimer;

		public DockPanelMdiManager()
		{	
            statusDisplayTimer = new Timer();
            statusDisplayTimer.Tick += new EventHandler(statusDisplayTimer_Tick);

            // Create the tree view;
            projectTreeView = new ProjectTreeView();
            this.projectTreeView.DockPanel = this.dockPanel;
            this.projectTreeView.HideOnClose = true;

            // Create the Toolbox;
            this.toolBox = new ToolBox();
            toolBox.DockPanel = this.dockPanel;
            this.toolBox.HideOnClose = true;
            
		}

        public DockPanel DockPanel
        {
            get { return this.dockPanel; }
            set { this.dockPanel = value; }
        }

		#region IMDIManager Members

		public void AddDocument(IMdiChild child)
		{
			if( child is DockContent)
			{
                DockContent docCont = child as DockContent;
                // Set the Parent;
                docCont.DockPanel = this.dockPanel;
                // Set the HideOnClose flag;
                docCont.HideOnClose = true;
                // TEMOPRARY WORKAROUND : remove this.
                docCont.CloseButton = false;
                if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
                {
                    docCont.MdiParent = this.dockPanel.Parent as Form;
                    docCont.Show();
                }
                else
                    docCont.Show(dockPanel);
			}
			else
			{
				MessageBox.Show("The MdiChild is not a DockContent, Cannot add.");
			}
		}

        public void RemoveDocument(IMdiChild mdiChild)
        {
            DockContent dockCont = mdiChild as DockContent;
            dockCont.HideOnClose = false;
            dockCont.Close();
        }

		public IMdiChild CurrentDocument
		{
			get
			{
                if (this.dockPanel.ActiveDocument is IMdiChild)
                    return this.dockPanel.ActiveDocument as IMdiChild;
                else
                    return null;
			}
			set
			{
				if ( value is DockContent )
				{
                    (value as DockContent).Show();
				}
			}
		}

        public IList<IMdiChild> Documents
		{
			get
			{
                IList<IMdiChild> mdiChildList = new List<IMdiChild>();
                foreach (IDockContent dockContent in this.dockPanel.Documents)
                {
                    if (dockContent is IMdiChild)
                        mdiChildList.Add(dockContent as IMdiChild);
                }
                return mdiChildList;
			}
		}

		public void ShowProjectTreeView(bool isVisible)
		{
			if (isVisible == true)
			{
                this.projectTreeView.Show(this.dockPanel, DockState.DockRight);
			}
			else
			{
				this.projectTreeView.Visible=false;
			}
		}

		public void ShowToolbox(bool isVisible)
		{
			if(isVisible == true)
			{
				this.toolBox.Show(dockPanel, DockState.DockLeft);
			}
			else
			{
				this.toolBox.Visible = false;
			}
		}

		public TreeView ProjectTreeView
		{
			get{return this.projectTreeView.TreeView;}
		}

		public void CloseAllDocuments()
		{
			foreach ( DockContent c in this.dockPanel.Documents)
			{
				c.Close();
			}
		}

        /// <summary>
        /// Set the application wide status bar control.
        /// </summary>
        public ToolStripStatusLabel StatusBar
        {
            set
            {
                this.statusLabel = value;
            }
            get
            {
                return this.statusLabel;
            }
        }

        public void ShowStatusMessage(string msg)
        {
            ShowStatusMessage(msg, 0);
        }

        public void ShowStatusMessage(string msg, int timeOutInMilliSeconds)
        {

            statusLabel.Text = msg;
            statusLabel.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            if (timeOutInMilliSeconds != 0)
            {
                statusDisplayTimer.Interval = timeOutInMilliSeconds;
                statusDisplayTimer.Enabled = true;
            }
        }

        public void ResetStatusMessage()
        {
            statusLabel.Text = "Ready";
            statusLabel.DisplayStyle = ToolStripItemDisplayStyle.Text;
            statusDisplayTimer.Enabled = false;
        }

		#endregion

        private void statusDisplayTimer_Tick(object sender, EventArgs e)
        {
            ResetStatusMessage();
        }

	}
}
